{
 "metadata": {
  "name": "",
  "signature": "sha256:05bc8f91994f40f9fb6d9775dd43c5e17a898f97da9ff28d69e425ba9b3c367a"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#\u0410\u0432\u0442\u043e\u0440: \u0414\u043c\u0438\u0442\u0440\u0438\u0439 \u041f\u0435\u0442\u0440\u043e\u0432\n",
      "\n",
      "from numpy import *\n",
      "\n",
      "#\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n",
      "q=open(\"spect.train\",\"r\")\n",
      "train = [ a.strip().split(\",\") for a in q]\n",
      "plus = [a for a in train if a[0]==\"1\"]\n",
      "minus = [a for a in train if a[0]==\"0\"]\n",
      "train_joined = [''.join(a) for a in train]\n",
      "\n",
      "#print('len train:', len(train))\n",
      "#print('len of one plus:', len(plus[0]), 'overall len plus:', len(plus))\n",
      "#print('len of one minus:', len(plus[0]), 'overall len minus: ', len(minus))\n",
      "q.close()\n",
      "\n",
      "#\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n",
      "q=open(\"spect.test\",\"r\")\n",
      "test = [ a.strip().split(\",\") for a in q]\n",
      "test_joined = [''.join(a) for a in test]\n",
      "q.close\n",
      "\n",
      "#print(len(test_joined))\n",
      "#for i in test_joined:\n",
      "#    print(i)\n",
      "\n",
      "#test1 = ''.join(train[0])\n",
      "#test2 = ''.join(train[1])\n",
      "#train_bin = []\n",
      "#print(test1)\n",
      "#print(bin(int(test1,2)))\n",
      "\n",
      "for i in range(0,len(train)):\n",
      "    temp = ''.join(train[i][1:])\n",
      "    train_bin.append(int(temp,2))\n",
      "\n",
      "#print(len(train_bin), len(train))\n",
      "plus_bin = train_bin[0:39]\n",
      "minus_bin = train_bin[40:79]\n",
      "\n",
      "#print(type(plus_bin[1]))\n",
      "\n",
      "#det_str = train[43][1:]\n",
      "#print(det_str)\n",
      "#to_detect = int(''.join(det_str))\n",
      "#print(to_detect)\n",
      "\n",
      "#\u0421\u043f\u0438\u0441\u043e\u043a \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0437\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\n",
      "vote_list = []\n",
      "\n",
      "#\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0444\u0430\u043b\u044c\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0433\u0438\u043f\u043e\u0442\u0435\u0437\n",
      "def falsify(mask_bin, context_bin):\n",
      "    for i in range(0,len(context_bin)-1):\n",
      "        if mask_bin & context_bin[i] == mask_bin:\n",
      "            return 0\n",
      "        else:\n",
      "            return 1\n",
      "\n",
      "#\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430\n",
      "def get_vote(to_detect, plus_bin, minus_bin):\n",
      "    vote_count = 0\n",
      "    for i in range(0,len(plus)-1):\n",
      "        mask = to_detect & plus_bin[i]\n",
      "        vote_count += falsify(mask, minus_bin)\n",
      "    for i in range(0,len(plus)-1):\n",
      "        mask = to_detect & minus_bin[i]\n",
      "        vote_count -= falsify(mask, plus_bin)\n",
      "    return vote_count\n",
      "\n",
      "#\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0437\u0430 \u043d\u0435\u0433\u043e\n",
      "for i in range(0, len(train)):\n",
      "    det_str = train[i][1:]\n",
      "    to_detect = int(''.join(det_str))\n",
      "    vote = get_vote(to_detect, plus_bin, minus_bin)\n",
      "    vote_list.append(vote)\n",
      "    #print('vote for object #', i, '('+''.join(det_str)+')', vote)\n",
      " \n",
      "    \n",
      "#\u041e\u0427\u0415\u041d\u042c \u0412\u0410\u0416\u041d\u042b\u0419 \u041a\u0423\u0421\u041e\u041a\n",
      "#\u0414\u0430\u043d\u043d\u044b\u0435 \u0432 \u044d\u0442\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0432\u044b: \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 train \u0438 \u043a\u0430\u043a \"+\", \u0438 \u043a\u0430\u043a \"-\".\n",
      "#\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u043b\u043e\u0436\u0438\u0442\u0441\u044f \u0432 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439.\n",
      "#\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u0438 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432.\n",
      "#\u0412\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043e\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0432\u044b\u0431\u043e\u0440\u043e\u0447\u043d\u043e\u0435 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 13.9487179487, \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u043e \u0432\u043f\u0440\u0430\u0432\u043e \u043e\u0442 \u043d\u0435\u0433\u043e. \n",
      "#\u0414\u043b\u044f \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043e\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u0440\u0435\u0434\u043d\u0435\u0435 8.925 \u0438 \u0441\u043c\u0435\u0449\u0435\u043d\u043e \u0432\u043b\u0435\u0432\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0433\u043e. \n",
      "#\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0435\u0448\u0430\u044e\u0449\u0435\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u044f \u0432\u0437\u044f\u043b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u043e\u043b\u043e\u0441\u043e\u0432 > 11. \u041f\u043e\u0447\u0435\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e 11? \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u0438\u0441\u043f\u0435\u0440\u0441\u0438\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0433\u043e\u043b\u043e\u0441\u043e\u0432\n",
      "#\u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 \u0431\u043b\u0438\u0436\u0435 \u043a \u0441\u0440\u0435\u0434\u043d\u0435\u043c\u0443 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445.\n",
      "\n",
      "#print(mean(vote_list[0:39]),std(vote_list[0:39])/sqrt(40))\n",
      "#print(mean(vote_list[40:89]),std(vote_list[40:89])/sqrt(40))\n",
      "\n",
      "#\u0421\u043b\u043e\u0432\u0430\u0440\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0445\u0440\u0430\u043d\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0443\u0433\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \n",
      "cv_res = {\n",
      " \"11\": 0,\n",
      " \"10\": 0,\n",
      " \"01\": 0,\n",
      " \"00\": 0,\n",
      " \"NaN\": 0,\n",
      "}\n",
      "\n",
      "#\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c: \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0442\u0435\u0441\u0442\u0430 \u0438 \u0443\u0433\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u0438\u0445.\n",
      "for i in test_joined:\n",
      "    det_str = i[1:]\n",
      "    to_detect = int(''.join(det_str))\n",
      "    vote = get_vote(to_detect, plus_bin, minus_bin)\n",
      "    if vote > 11:\n",
      "        decision = '1'\n",
      "    else:\n",
      "        decision = '0'\n",
      "    if i[0] == '1' and decision == '1':\n",
      "        cv_res['11'] +=1\n",
      "    elif i[0] == '1' and decision == '0':\n",
      "        cv_res['10'] +=1\n",
      "    elif i[0] == '0' and decision == '1':\n",
      "        cv_res['01'] +=1\n",
      "    elif i[0] == '0' and decision == '0':\n",
      "        cv_res['00'] +=1\n",
      "    else:\n",
      "        cv_res['NaN'] +=1\n",
      "\n",
      "print('accuracy: ', (cv_res['11']+cv_res['00'])/(cv_res['11']+cv_res['00']+cv_res['01']+cv_res['10']+cv_res['NaN']))\n",
      "print('true-positive: ', cv_res['11'], 'true-negative: ', cv_res['00'])\n",
      "print('false-negative: ', cv_res['10'], 'false-positive: ', cv_res['01'])\n",
      "print('uncertain:', cv_res['NaN'])\n",
      "\n",
      "#\u0418\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0448\u043b\u0430 78,6%. \u042f \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u0434\u043b\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u044d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043a\u0430\u0437\u0430\u043d\u043e,\n",
      "#\u0447\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043a\u0440\u0443\u0442\u043e\u0439 (\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439) \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u043c\u0435\u0435\u0442 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043e\u043a\u043e\u043b\u043e 84%. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043c\u044b \u0443\u043b\u043e\u0432\u0438\u043b\u0438. \n",
      "#\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0445\u043e\u0440\u043e\u0448\u043e, \u0447\u0442\u043e \u0443\u0440\u043e\u0432\u0435\u043d\u044c false-positive \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 3.6%. \u0414\u043b\u044f \u0442\u0440\u0430\u0441\u043f\u043b\u0430\u043d\u0442\u0430\u043b\u043e\u0433\u0438\u0438 \u044d\u0442\u043e \u0432\u0430\u0436\u043d\u043e, \u0432\u0435\u0434\u044c \u043f\u0435\u0440\u0435\u0441\u0430\u0434\u043a\u0430 \n",
      "#\u043d\u0435\u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e \u043e\u0440\u0433\u0430\u043d\u0430 \u0432\u0440\u0435\u0434\u0438\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043e\u0442\u043a\u0430\u0437 \u043e\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043f\u043b\u043e\u0445\u043e\u0439 \u043f\u0435\u0440\u0435\u0441\u0430\u0434\u043a\u0438 (\u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0449\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0434\u043e\u043d\u043e\u0440)."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "accuracy:  0.786096256684492\n",
        "true-positive:  139 true-negative:  8\n",
        "false-negative:  33 false-positive:  7\n",
        "uncertain: 0\n"
       ]
      }
     ],
     "prompt_number": 195
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}